<template>
  <div class="home">
    <div class="title-container">
      <div class="search-content">
        <el-form :model="searchModel" :inline="true">
          <el-form-item>
            <el-input
              v-model="searchModel.userName"
              placeholder="请输入名称"
              prefix-icon="el-icon-search"
            />
          </el-form-item>
        </el-form>
        <div class="search-btn">
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="searchClick"
            size="medium"
          >搜索
          </el-button>
        </div>
      </div>
    </div>

    <el-card>
      <div class="addAccount">
        <perButton label="新建" size="medium" icon="el-icon-plus" perms="PC_SYS_BULD_ADD" type="primary"
                   @click="handlerAdd"></perButton>
        <el-button
          type="success"
          icon="el-icon-refresh"
          size="medium"
          @click="refresh"
        >刷新
        </el-button>
      </div>
      <el-table
        v-loading="loading"
        :data="tableData"
        border
        style="width: 100%;"
        class="elTable"
      >
        <el-table-column type="index" label="序号" width="50" align="center"/>
        <el-table-column prop="systemCode" label="APP编码" align="center"/>
        <el-table-column prop="systemName" label="APP名称" align="center"/>
        <el-table-column prop="systemRefCode" label="APP系统识别码" align="center"/>
        <el-table-column prop="systemDes" label="APP描述" show-overflow-tooltip align="center"/>
        <el-table-column prop="systemRefCode" label="分配人员" align="center"/>
        <el-table-column prop="createByName" label="创建人" align="center"/>
        <el-table-column label="创建时间" align="center" prop="createTime">
          <template slot-scope="scope">
            {{ scope.row.createTime }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="400" align="center">
          <template #default="{ row }">
            <perButton label="配置顶部" type="primary" size="mini" perms="PC_SYS_BULD_TOP"
                       @click="settingTop(row)"></perButton>
            <perButton label="配置菜单" type="primary" size="mini" perms="PC_SYS_BULD_MENU"
                       @click="settingMenu(row)"></perButton>
            <perButton label="编辑" type="warning" size="mini" perms="PT_USER_EDIT"
                       @click="handlerEdit(row)"></perButton>
            <perButton label="删除" type="danger" size="mini" perms="PT_USER_DEL"
                       @click="handlerDelete(row)"></perButton>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        class="pagination"
        :total="count"
        :page.sync="pageIndex"
        :limit.sync="pageSize"
        @pagination="getAccountData"
      />
    </el-card>

    <!-- 新建弹框 -->
    <yc-dialog ref="addDialog" :centerTitle="centerTitle" :dialogTitle="dialogTitle">
      <template #content>
        <div class="dialog-content">
          <el-form
            ref="addFormRef"
            :model="addForm"
            :rules="rules"
            label-width="100px"
          >
            <el-form-item label="APP名称" prop="systemName">
              <el-input v-model="addForm.systemName"/>
            </el-form-item>
            <el-form-item label="APP识别码" prop="systemRefCode">
              <el-input v-model="addForm.systemRefCode"/>
            </el-form-item>
            <el-form-item label="APP描述">
              <el-input v-model="addForm.systemDes" type="textarea" :rows="2"/>
            </el-form-item>
          </el-form>
        </div>
      </template>
      <template #footer>
        <el-button type="primary" :loading="addLoading" @click="addSave">确定</el-button>
        <el-button @click="$refs.addDialog.dialogVisible = false">取消
        </el-button>
      </template>
    </yc-dialog>
  </div>
</template>

<script>
  import ycDialog from "@/components/pageDialog"
  import perButton from "@/components/permissionButton"
  import { queryPage, updateByModel, deleteByModel, insertByModel } from "@/api/systemBuilding/appSystemBuilding"

  export default {
    components: {
      ycDialog,
      perButton
    },
    data() {
      return {
        projectCode: "",
        tableData: [],
        count: 0,
        pageIndex: 1,
        pageSize: 10,
        loading: false,
        searchModel: {
          userName: "",
        },

        centerTitle: "",
        dialogTitle: "",
        addLoading: false,

        addForm: {
          id: 0,
          systemName: "",
          systemRefCode: "",
          systemDes: "",
        },
        rules: {
          systemName: [
            {required: true, message: "请输入APP名称", trigger: "blur"}
          ],
          systemRefCode: [
            {required: true, message: "请输入APP识别码", trigger: "blur"}
          ],
        },

        topLoading: false,
      }
    },
    created() {
      this.projectCode = sessionStorage.getItem('projectCode')

      this.getAccountData()
    },
    methods: {
      /**
       * 获取表格方法
       */
      async getAccountData(tableInfo) {
        this.loading = true
        const {object} = await queryPage({
          projectCode: this.projectCode,
          pageIndex: this.pageIndex,
          pageSize: this.pageSize
        })
        this.tableData = object.data
        this.count = object.count
        this.loading = false
      },

      pagination(pageItem) {
        this.getAccountData()
      },


      // 搜索按钮
      async searchClick() {
        this.getAccountData(this.searchModel)
      },

      /**
       * 删除按钮
       */
      async handlerDelete({id, userCode}) {
        this.$confirm("确定删除此数据吗", "删除操作", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "error",
          center: true
        }).then(async () => {
          const {message, status} = await deleteByModel({id, userCode})
          if (status == "200") {
            this.getAccountData()
            this.isGlobalMessageShow = true
            this.message = "success"
          } else {
            this.message = "error"
          }
        })
      },

      /**
       * 新建
       */
      handlerAdd() {
        this.dialogTitle = "新建"
        this.centerTitle = "新建"

        this.addLoading = false
        this.$refs.addDialog.dialogVisible = true

        Object.keys(this.addForm).forEach((item) => {
          this.addForm[item] = ""
        })
      },
      /**
       * 修改
       */
      handlerEdit(row) {
        this.dialogTitle = "编辑"
        this.centerTitle = "编辑"

        this.addForm.id = row.id
        this.addForm.systemName = row.systemName
        this.addForm.systemRefCode = row.systemRefCode
        this.addForm.systemDes = row.systemDes

        this.addLoading = false
        this.$refs.addDialog.dialogVisible = true
      },
      /**
       * 新建保存
       */
      addSave() {
        this.$refs.addFormRef.validate(async (v) => {
          if (v) {
            this.addLoading = true;

            if (this.addForm.id) {
              const {message, status} = await updateByModel({...this.addForm, projectCode: this.projectCode,});
              if (status == 200) {
                this.getAccountData()
                this.$refs.addDialog.dialogVisible = false
                this.$message.success(message)
              }
            } else {
              const {message, status} = await insertByModel({...this.addForm, projectCode: this.projectCode,});
              if (status == 200) {
                this.getAccountData()
                this.$refs.addDialog.dialogVisible = false
                this.$message.success(message)
              }
            }

            this.addLoading = false;
          }
        })
      },


      /**
       * 刷新
       */
      refresh() {
        this.searchModel = {
          userName: "",
        }
        this.getAccountData()
      },
    }
  }
</script>

<style lang="scss" scoped>
  .title-container {
    display: flex;
    justify-content: flex-end;
    height: 60px;

    .search-content {
      margin-left: 20px;
      display: flex;
    }

    .addAccount {
      margin: 10px 20px;
    }
  }

  .pagination {
    display: flex;
    justify-content: flex-end;
  }

  .footer {
    height: 360px;
    display: flex;
    align-items: flex-end;
    padding: 5px 20px;
  }

  .bgcss {
    width: 160px;
    height: 90px;
    cursor: pointer;
  }

  .dialog-content {
    ::v-deep.el-radio__label {
      display: none;
    }
  }

  .dialog-content2 {
    .el-radio {
      display: flex;
      flex-direction: column-reverse;
      align-items: center;

      ::v-deep.el-radio__label {
        margin-bottom: 5px;
      }
    }
  }
</style>

